<?php
/**
 * ClassName: MedicineService
 * Description:
 * @Author 周昌
 * @Create 2023/11/18 10:06
 */

namespace service\admin;


use libs\Tool;
use model\MedicineModel;
use service\BaseService;
use think\Db;
use think\Model;

class MedicineService extends BaseService
{
    protected static $model='model\MedicineModel';


    /**
     * #User: Administrator
     * #Time: 2023/11/18 11:24
     * #Author:周昌
     * #Description: 单条更新药品库存
     * @param $where
     * @param $quantity 数量
     * @param int $type 0：减少 1：增加
     * @return array
     */
    public static function updateInventory($where,$quantity,$type=1)
    {
        Db::startTrans();
        try {
            //使用行锁锁定该行数据
            $res=self::$model::where($where)->lock(true)->find();
            if($type){
                //自增
                $result=self::$model::where($where)->setInc('inventory',$quantity);
            }else{
                //自减少
                if($res['inventory']<$quantity){
                    Db::rollback();
                    return Tool::Error('库存不足,当前库存为'.$res['inventory']);
                }
                $result=self::$model::where($where)->setDec('inventory',$quantity);
            }
            if ($res&&$result){
                Db::commit();
                return Tool::Success('药品库存更新成功');
            }else{
                Db::rollback();
                return Tool::Error('药品库存更新失败');
            }
        }catch (\Exception $e){
            Db::rollback();
            return Tool::Error($e->getMessage());
        }


    }

    /**
     * #User: Administrator
     * #Time: 2023/12/20 13:14
     * #Author:周昌
     * #Description: 批量修改库存
     * @param array $medicine_ids 药品主键id数组
     * @param array $quantitys  数量数组
     * @param int $type 0：减少 1：增加
     * @return array
     */
    public static function updateMoreInventory($medicine_ids,$quantitys,$type=1)
    {
        $data=[];
        $msg='';
        $MdecineM=new MedicineModel();
        Db::startTrans();
        try {
            //使用行锁锁定多行数据
            $res=self::$model::where('id','in',$medicine_ids)->lock(true)->select();
            foreach ($res as $k=>$v){
                $row=[];
                foreach ($medicine_ids as $m=>$n){
                    if($v['id']==$n){
                        $row['id']=$n;
                        $row['inventory']=$type?bcadd($v['inventory'],$quantitys[$m]):bcsub($v['inventory'],$quantitys[$m]);
                        //减库存判断 库存是否充足
                        if($row['inventory']<0){
                            $msg.=$v['name'].'库存不足当前库存'.$v['inventory'].'/';
                        }
                    }
                }
                $data[]=$row;
            }
            if(!empty($msg)){
                Db::rollback();
                return Tool::Error($msg);
            }

            $result=$MdecineM->saveAll($data);

            if ($res&&$result!==false){
                Db::commit();
                return Tool::Success('药品库存更新成功');
            }else{
                Db::rollback();
                return Tool::Error('药品库存更新失败');
            }
        }catch (\Exception $e){
            Db::rollback();
            return Tool::Error($e->getMessage());
        }


    }
    //列表数据
    public static function getAjaxData($where)
    {
        $table = self::$model::order('id desc');
        return self::tableJsonData($table, $where, function ($data){
            if($data['inventory']<=$data['alert_inventory']){
                $data['inventory_style']=["color"=>"red"];
            }else{
                $data['inventory_style']=["color"=>"green"];
            }
            $data['hospital_name'] = $data['hospital_id']?HospitalService::getNameByKey($data['hospital_id'],''):'';
            return $data;
        });
    }

}